import { createRouter, createWebHistory } from "vue-router";
import { useJinnongStore } from "../stores/jinnong";
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "home",
      component: () => import("../views/HomeView.vue"),
    },
    {
      path: "/device",
      name: "device",
      component: () => import("../views/DeviceView.vue"),
    },
    {
      path: "/risk",
      name: "risk",
      component: () => import("../views/RiskView.vue"),
    },
    {
      path: "/illegal",
      name: "illegal",
      component: () => import("../views/IllegalView.vue"),
    },
  ],
});

router.beforeEach(async (to, from) => {
  const store = useJinnongStore();
  if (to.name != "illegal") {
    if (to.query.token) {
      store.token = to.query.token;
    } else {
      return "/illegal";
    }
  }
});

export default router;
